/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package me.zhengjie.modules.cspimp.psp.rest;

import me.zhengjie.annotation.Log;
import me.zhengjie.modules.cspimp.psp.domain.TargetResponsibility;
import me.zhengjie.modules.cspimp.psp.domain.vo.TargetResponsibilityVo;
import me.zhengjie.modules.cspimp.psp.service.TargetResponsibilityService;
import me.zhengjie.modules.cspimp.psp.service.dto.TargetResponsibilityQueryCriteria;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;

import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
 * @author zmm
 * @website https://docs.auauz.net
 * @date 2020-05-31
 **/
@RestController
@RequiredArgsConstructor
@Api(tags = "目标责任管理")
@RequestMapping("/api/targetResponsibility")
public class TargetResponsibilityController {

    private final TargetResponsibilityService targetResponsibilityService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('targetResponsibility:list')")
    public void download(HttpServletResponse response, TargetResponsibilityQueryCriteria criteria) throws IOException {
        targetResponsibilityService.download(targetResponsibilityService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询目标责任")
    @ApiOperation("查询目标责任")
    @PreAuthorize("@el.check('targetResponsibility:list')")
    public ResponseEntity<Object> query(TargetResponsibilityQueryCriteria criteria, Pageable pageable) {
        return new ResponseEntity<>(targetResponsibilityService.queryAll(criteria, pageable), HttpStatus.OK);
    }

    @PostMapping
    @Log("新增目标责任")
    @ApiOperation("新增目标责任")
    @PreAuthorize("@el.check('targetResponsibility:add')")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dutyLevel", value = "责任级别", required = true, dataType = "String", paramType = "body"),
            @ApiImplicitParam(name = "dutyName", value = "责任名称", required = true,dataType = "Integer", paramType = "body"),
            @ApiImplicitParam(name = "dutyUserId", value = "责任人", dataType = "Integer", paramType = "body"),
            @ApiImplicitParam(name = "dutyDeptId", value = "责任部门", dataType = "Integer", paramType = "body"),
            @ApiImplicitParam(name = "dutyTarget", value = "责任目标", dataType = "Integer", paramType = "body"),
            @ApiImplicitParam(name = "dutyDesc", value = "责任描述", dataType = "Integer", paramType = "body"),
            @ApiImplicitParam(name = "filePath", value = "文件路径", dataType = "Integer", paramType = "body"),
            @ApiImplicitParam(name = "formulateDate", value = "制定日期", dataType = "Integer", paramType = "body"),
            @ApiImplicitParam(name = "auditDate", value = "审核日期", dataType = "Integer", paramType = "body"),
            @ApiImplicitParam(name = "approvalDate", value = "批准日期", dataType = "Integer", paramType = "body"),
    })
    public ResponseEntity<Object> create(@Validated @RequestBody TargetResponsibilityVo resources) {
        return new ResponseEntity<>(targetResponsibilityService.create(resources), HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改目标责任")
    @ApiOperation("修改目标责任")
    @PreAuthorize("@el.check('targetResponsibility:edit')")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "String", paramType = "body"),
            @ApiImplicitParam(name = "dutyLevel", value = "责任级别", dataType = "String", paramType = "body"),
            @ApiImplicitParam(name = "dutyName", value = "责任名称", dataType = "Integer", paramType = "body"),
            @ApiImplicitParam(name = "dutyUserId", value = "责任人", dataType = "Integer", paramType = "body"),
            @ApiImplicitParam(name = "dutyDeptId", value = "责任部门", dataType = "Integer", paramType = "body"),
            @ApiImplicitParam(name = "dutyTarget", value = "责任目标", dataType = "Integer", paramType = "body"),
            @ApiImplicitParam(name = "dutyDesc", value = "责任描述", dataType = "Integer", paramType = "body"),
            @ApiImplicitParam(name = "filePath", value = "文件路径", dataType = "Integer", paramType = "body"),
            @ApiImplicitParam(name = "formulateDate", value = "制定日期", dataType = "Integer", paramType = "body"),
            @ApiImplicitParam(name = "auditDate", value = "审核日期", dataType = "Integer", paramType = "body"),
            @ApiImplicitParam(name = "approvalDate", value = "批准日期", dataType = "Integer", paramType = "body"),
    })
    public ResponseEntity<Object> update(@Validated @RequestBody TargetResponsibilityVo resources) {
        targetResponsibilityService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除目标责任")
    @ApiOperation("删除目标责任")
    @PreAuthorize("@el.check('targetResponsibility:del')")
    @DeleteMapping
    public ResponseEntity<Object> delete(@RequestBody Long[] ids) {
        targetResponsibilityService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}